/* ----------------------------------------------------------------------------
* File: skype_quiz.cpp
*
* Desc: Thread function for quiz
*
* Created: 31/03/2010, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#include "main.h"
#include "quiz_inc.h"

using namespace SKYPE4COMLib;
using namespace std;

#define EVENT_STOP_TIMER		0
#define EVENT_START_QUESTION	1
#define EVENT_STOP_QUESTION		2
#define EVENT_ANSWER			3



DWORD WINAPI QuizTimerThread(LPVOID lpParam)
{
	
}


/************************************************************************/
/*	Create timer thread for quiz                                        */
/************************************************************************/
int CreateTimerThread(SkypeEnv *pEnv)
{
	DWORD dwTimerThreadId;

	// Create start event for timer thread
	pEnv->hStartThreadEvents[QUIZ_TIMER_THREAD_NUM] = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (pEnv->hStartThreadEvents[QUIZ_TIMER_THREAD_NUM] == NULL)
	{
		MyOutputDebugString(TEXT("skype_plugin: QuizThread() - ERROR: Unable to create start event for timer thread"));
		return SKYPE_PLUG_ERROR;
	}

	// Create event for stop timer thread
	pEnv->hTimerEvents[0] = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (pEnv->hTimerEvents[0] == NULL)
	{
		MyOutputDebugString(TEXT("skype_plugin: QuizThread() - ERROR: Unable to create event for stop timer thread"));
		return SKYPE_PLUG_ERROR;
	}

	// Create event for starting question
	pEnv->hTimerEvents[1] = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (pEnv->hTimerEvents[1] == NULL)
	{
		MyOutputDebugString(TEXT("skype_plugin: QuizThread() - ERROR: Unable to create start question event"));
		return SKYPE_PLUG_ERROR;
	}

	// Create event for stopping question
	pEnv->hTimerEvents[2] = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (pEnv->hTimerEvents[2] == NULL)
	{
		MyOutputDebugString(TEXT("skype_plugin: QuizThread() - ERROR: Unable to create stop question event"));
		return SKYPE_PLUG_ERROR;
	}

	// Create event for answer
	pEnv->hTimerEvents[3] = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (pEnv->hTimerEvents[3] == NULL)
	{
		MyOutputDebugString(TEXT("skype_plugin: QuizThread() - ERROR: Unable to create event for answer"));
		return SKYPE_PLUG_ERROR;
	}

	// Create timer thread
	pEnv->hTimerThread = CreateThread(NULL, 0, QuizTimerThread, (LPVOID)pEnv, 0, &dwTimerThreadId);
	if (pEnv->hTimerThread == NULL)
	{
		CloseHandle(pEnv->hStartThreadEvents[QUIZ_TIMER_THREAD_NUM]);
		MyOutputDebugString(TEXT("skype_plugin: QuizThread() - ERROR: Unable to create quiz timer thread"));
		return SKYPE_PLUG_ERROR;
	}
	// Wait start of timer thread
	WaitForSingleObject(pEnv->hStartThreadEvents[QUIZ_TIMER_THREAD_NUM], INFINITE);

	return SKYPE_PLUG_OK;
}


/************************************************************************/
/*	Thread function for quiz                                            */
/************************************************************************/
DWORD WINAPI QuizThread(LPVOID lpParam)
{
	DWORD dwTimerThreadId;
	std::string question, answer;

	SkypeEnv *pEnv = reinterpret_cast<SkypeEnv*>(lpParam);
	if (pEnv == NULL)
		ExitThread(0);

	ISkypePtr pSkype = pEnv->skypeInfo.pSkype;
	MyOutputDebugString(TEXT("skype_plugin: QuizThread() - start"));

	SetEvent(pEnv->hStartThreadEvents[QUIZ_THREAD_NUM]);

#ifdef _DEBUG
	MyOutputDebugString(TEXT("skype_plugin: QuizThread() - Create quiz object"));
#endif

	// Create quiz object
	IQuiz *quiz = CreateQuiz();

	QuizParams params;
	memset(&params, 0, sizeof(params));
	params.dictFile = pEnv->question_file.c_str();
	params.userFile = pEnv->user_file.c_str();

#ifdef _DEBUG
	MyOutputDebugString(TEXT("skype_plugin: QuizThread() - Init quiz object"));
#endif

	// Init quiz object
	if (quiz->Init(params) != QUIZ_OK)
	{
		MyOutputDebugString(TEXT("skype_plugin: QuizThread() - ERROR: Unable to init quiz object"));
		ExitThread(0);
	}

#ifdef _DEBUG
	MyOutputDebugString(TEXT("skype_plugin: QuizThread() - Create timer thread"));
#endif

	// Create timer thread
	if (CreateTimerThread(pEnv) != SKYPE_PLUG_OK)
		ExitThread(0);


	// Main cycle
	while(1)
	{
		question = quiz->GetQuestion();
		answer = quiz->GetAnswer();
		
		// send question
		pEnv->chatInfo.pChat->SendMessage(question.c_str());
		SetEvent(pEnv->hTimerEvents[EVENT_START_QUESTION]);

		// wait timer event
		//WaitForSingleObject(pEnv->hTimerEvent, INFINITE);

		// send answer
		pEnv->chatInfo.pChat->SendMessage(answer.c_str());
	}


	MyOutputDebugString(TEXT("skype_plugin: QuizThread() - finish"));

	CloseHandle(pEnv->hStartThreadEvents[QUIZ_TIMER_THREAD_NUM]);
	CloseHandle(pEnv->hTimerThread);
	ExitThread(0);
}

